package com.denlaku.longan.service.impl;

import com.denlaku.longan.qo.ObjPermQuery;
import com.denlaku.longan.repository.ObjectTagRelRepository;
import com.denlaku.longan.service.ObjectTagRelService;
import com.denlaku.longan.vo.ObjectTagRel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author tianx
 */
@Slf4j
@Service
public class ObjectTagRelServiceImpl implements ObjectTagRelService {
    @Autowired
    private ObjectTagRelRepository objectTagRelRepository;

    @Override
    public void batchAdd(Long tenantId, List<ObjectTagRel> relList) {
        objectTagRelRepository.batchAdd(tenantId, relList);
    }

    @Override
    public List<ObjectTagRel> list(ObjPermQuery query) {
        List<Long> tagSetIds = new ArrayList<>(ListUtils.emptyIfNull(query.getTagSetIds()));
        Optional.ofNullable(query.getTagSetId()).ifPresent(tagSetIds::add);
        query.setTagSetIds(tagSetIds);
        if (tagSetIds.isEmpty()) {
            return List.of();
        }
        return objectTagRelRepository.list(query);
    }

    @Override
    public void delete(ObjPermQuery query) {
        objectTagRelRepository.delete(query);
    }
}
